package com.booktrading.controller;

import com.booktrading.model.Book;
import com.booktrading.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.HashSet;
import java.util.Set;

/**
 * 书籍列表控制器
 */
@Controller
@RequestMapping("/books")
public class BookListController {

    @Autowired
    private BookService bookService;
    
    /**
     * 书籍列表页面，支持多种筛选和排序
     */
    @GetMapping("/list")
    public String listBooks(
            Model model,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "author", required = false) String author,
            @RequestParam(value = "minPrice", required = false) Double minPrice,
            @RequestParam(value = "maxPrice", required = false) Double maxPrice,
            @RequestParam(value = "condition", required = false) Integer condition,
            @RequestParam(value = "sortBy", required = false, defaultValue = "createTime") String sortBy,
            @RequestParam(value = "sortDirection", required = false, defaultValue = "desc") String sortDirection,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "12") Integer size) {

        // 重定向到 /book/list
        return "redirect:/book/list";
    }
    
    /**
     * 获取所有书籍分类列表
     */
    private List<String> getAllCategories() {
        // 从所有在售书籍中提取分类
        List<Book> books = bookService.getAllOnSaleBooks();
        Set<String> categories = new HashSet<>();
        
        for (Book book : books) {
            if (book.getCategory() != null && !book.getCategory().isEmpty()) {
                categories.add(book.getCategory());
            }
        }
        
        return categories.stream().collect(Collectors.toList());
    }
    
    /**
     * 获取价格区间列表
     */
    private List<PriceRange> getPriceRanges() {
        return List.of(
            new PriceRange(0.0, 10.0, "10元以下"),
            new PriceRange(10.0, 30.0, "10-30元"),
            new PriceRange(30.0, 50.0, "30-50元"),
            new PriceRange(50.0, 100.0, "50-100元"),
            new PriceRange(100.0, null, "100元以上")
        );
    }
    
    /**
     * 获取书籍成色列表
     */
    private List<BookCondition> getConditions() {
        return List.of(
            new BookCondition(1, "全新"),
            new BookCondition(2, "九成新"),
            new BookCondition(3, "八成新"),
            new BookCondition(4, "七成新"),
            new BookCondition(5, "六成新及以下")
        );
    }
    
    /**
     * 价格区间内部类
     */
    public static class PriceRange {
        private Double min;
        private Double max;
        private String display;
        
        public PriceRange(Double min, Double max, String display) {
            this.min = min;
            this.max = max;
            this.display = display;
        }
        
        public Double getMin() {
            return min;
        }
        
        public Double getMax() {
            return max;
        }
        
        public String getDisplay() {
            return display;
        }
    }
    
    /**
     * 书籍成色内部类
     */
    public static class BookCondition {
        private Integer value;
        private String display;
        
        public BookCondition(Integer value, String display) {
            this.value = value;
            this.display = display;
        }
        
        public Integer getValue() {
            return value;
        }
        
        public String getDisplay() {
            return display;
        }
    }
} 